package com.tianyang.yukuaifuzs.business.main.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.business.main.bean.PosRateOutput;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Description：费率配置
 * Created by xyj on 2024/3/26
 */
public class RateConfigsAdapter extends BaseQuickAdapter<PosRateOutput, BaseViewHolder> {

    private boolean isRateConfig;

    public RateConfigsAdapter(boolean isRateConfig) {
        super(R.layout.item_rate_config, new ArrayList<>());
        this.isRateConfig = isRateConfig;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, PosRateOutput item) {
        if (item != null) {
            helper.setText(R.id.productNameTv, VerifyUtil.checkString(item.getProductName()))
                    .setText(R.id.showRateTv, VerifyUtil.checkString(item.getChannelRate()))
                    .setText(R.id.showExtraTv, VerifyUtil.checkString(item.getChannelSettlement()))
                    .setText(R.id.rateTv, item.getChannelRateMin() + "~" + item.getChannelRateMax())
                    .setText(R.id.extraTv, item.getChannelSettlementMin() + "~" + item.getChannelSettlementMax());
            EditText rateEt = helper.getView(R.id.rateEt);
            EditText extraEt = helper.getView(R.id.extraEt);
            if (!TextUtils.isEmpty(item.getChannelRate())) {
                rateEt.setText(item.getChannelRate());
            }
            if (!TextUtils.isEmpty(item.getChannelSettlement())) {
                extraEt.setText(item.getChannelSettlement());
            }
            rateEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if(hasFocus){
                        rateEt.addTextChangedListener(new TextSwitcher(helper.getLayoutPosition(), rateEt, false));
                    }else{
                        rateEt.removeTextChangedListener(new TextSwitcher(helper.getLayoutPosition(), rateEt, false));
                    }
                }
            });
            extraEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if(hasFocus){
                        extraEt.addTextChangedListener(new TextSwitcher(helper.getLayoutPosition(), extraEt, true));
                    }else{
                        extraEt.removeTextChangedListener(new TextSwitcher(helper.getLayoutPosition(), extraEt, true));
                    }
                }
            });

            LinearLayout showRateLl = helper.getView(R.id.showRateLl);
            LinearLayout editRateLl = helper.getView(R.id.editRateLl);
            if (isRateConfig) {
                showRateLl.setVisibility(View.GONE);
                editRateLl.setVisibility(View.VISIBLE);
            } else {
                showRateLl.setVisibility(View.VISIBLE);
                editRateLl.setVisibility(View.GONE);
            }
            View topView = helper.getView(R.id.topView);
            if (helper.getLayoutPosition() == 0) {
                topView.setVisibility(View.VISIBLE);
            } else {
                topView.setVisibility(View.GONE);
            }
        }
    }

    @NonNull
    @Override
    public List<PosRateOutput> getData() {
        return super.getData();
    }

    class TextSwitcher implements TextWatcher {

        private int position;

        private EditText editText;

        private boolean isExtra;

        TextSwitcher(int position, EditText editText, boolean isExtra) {
            this.position = position;
            this.editText = editText;
            this.isExtra = isExtra;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(editText.isFocused()){
                if (isExtra) {
                    getData().get(position).setChannelSettlement(s.toString());
                } else {
                    getData().get(position).setChannelRate(s.toString());
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
